package com.strawcom.charmring.util;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.google.gson.Gson;
import com.strawcom.charmring.App;
import com.strawcom.charmring.C;
import com.strawcom.charmring.HttpsTrustManager;
import com.strawcom.charmring.bean.UserInfo;
import com.strawcom.charmring.bean.http.BaseRequest;
import com.strawcom.charmring.bean.http.BaseResponse;
import com.strawcom.charmring.bean.http.GetContactInfoRequest;
import com.strawcom.charmring.bean.http.RankRequest;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * 
 * @ClassName: HttpUtil
 * @Description:访问网络工具类
 * @author liyongqiang
 * @date 2015-3-30 上午12:03:01
 * 
 */
public class HttpUtil {
	
	/*public final static String url = "http://srvdata.strawcom.com/SmartRing/msgid=";
	public final static String url_img = "http://srvdata.strawcom.com/SmartRing/images/";
	public final static String url_update = "http://srvdata.strawcom.com/SmartRing/imgs/";
	public final static String url_app_update = "http://srvdata.strawcom.com/SmartRing/imgs/";*/
	
	
	//my mandation:change http url to htts url-------------------------------------------
	/*public final static String url = "https://srvdata.strawcom.com/SmartRing/msgid=";
	public final static String url_img = "https://srvdata.strawcom.com/SmartRing/images/";
	public final static String url_update = "https://srvdata.strawcom.com/SmartRing/imgs/";
	public final static String url_app_update = "https://srvdata.strawcom.com/SmartRing/imgs/";*/
	
	public final static boolean http = false;
	
	public final static String url = URLProvider.getUrl(http);
	public final static String url_img = URLProvider.getUrl_img(http);
	public final static String url_update = URLProvider.getUrl_update(http);
	public final static String url_app_update = URLProvider.getUrl_app_update(http);
	
	
//	public final static String url = "http://192.168.1.54:8080/SmartRing/msgid=";
//	public final static String url_img = "http://192.168.1.54:8080/SmartRing/images/";
//	public final static String url_update = "http://192.168.1.54:8080/SmartRing/imgs/";
//	public final static String url_app_update = "http://192.168.1.54:8080/SmartRing/imgs/";
	
	public final static Gson gson=new Gson();

	public interface ResponeListener {

		public void onResponse(BaseResponse response,String resultStr);

		public void onErrorResponse(Exception error);
	}
	
	public static void requestJson(int requestType,final BaseRequest request,final ResponeListener listener){
		String requestUrl=url+Base64Code.encode(request.msgid.getBytes());
		if(C.request_msgid_get_user_icon.equals(request.msgid)){
			requestUrl+="?phone="  +((UserInfo)request).getPhone();
		}else if(C.request_msgid_get_contact_info.equals(request.msgid)){
			String cks =((GetContactInfoRequest)request).ck;
			requestUrl+="?ck="+cks;
		}else if(C.request_msgid_get_sports_ranking.equals(request.msgid)){
			requestUrl+="?date="+((RankRequest)request).date;
		}
		String requestParame=gson.toJson(request);
		LogUtil.d("HttpUtil request:" + request.msgid+" url:"+requestUrl);
		LogUtil.d("HttpUtil request:" + request.msgid+" params:"+requestParame);
		JsonObjectRequest req=null;
		try {
			req = new JsonObjectRequest(requestType,requestUrl, new JSONObject(requestParame),
			       new Response.Listener<JSONObject>() {
			           @Override
			           public void onResponse(JSONObject response) {
			        	   String responseJson=response.toString();
			        	   LogUtil.d("HttpUtil response:" + request.msgid+" params:"+responseJson);
			        	   listener.onResponse(gson.fromJson(responseJson,BaseResponse.class),responseJson);
			           }
			       }, new Response.ErrorListener() {
			           @Override
			           public void onErrorResponse(VolleyError error) {
			        	   LogUtil.d("HttpUtil response:" + request.msgid+" error:"+error.getMessage());
			        	   error.printStackTrace();
			               listener.onErrorResponse(error);
			           }
			       }){
				@Override
				public Map<String, String> getHeaders()
						throws AuthFailureError {
					Map<String, String> handles=null;
					
					if(request.msgid.equals(C.request_msgid_post_user_password)){
					
					}else if(!request.msgid.equals(C.request_msgid_post_user_regist)) {
						handles= new HashMap<String, String>();
						String up="";
						if(request.msgid.equals(C.request_msgid_get_user_signin)){
							up=((UserInfo)request).getPhone() +":"+((UserInfo)request).getPassword();
						}
						else if(request.msgid.equals(C.request_msgid_post_user_password)){
							up=((UserInfo)request).getPhone() +":"+((UserInfo)request).getPassword();
						}
						else{
							up=PreferenceUtil.readString(App.i(),C.sp.phone)+":"+PreferenceUtil.readString(App.i(),C.sp.password);
						}
						String str=Base64Code.encode(up.getBytes());
						handles.put("Authorization","basic "+str);
					}
					if(handles==null){
						return super.getHeaders();
					}else{
						return handles;
					}
				}
			};
			
			HttpsTrustManager.allowAllSSL();
			App.i().mQueue.add(req);
		} catch (Exception error) {
			error.printStackTrace();
			LogUtil.d(request.msgid+":"+error.getMessage());
			listener.onErrorResponse(error);
		}
	}

	
}
